package sql.impl;

import annotation.Column;
import annotation.Table;
import entity.User;
import sql.SqlUtil;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;

public class MysqlUtil implements SqlUtil {

    @Override
    public String query(User user) throws ClassNotFoundException, IllegalAccessException {
        Class<?> userClass = user.getClass();
        Field[] fields = userClass.getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
        }
        int id = (int)fields[0].get(user);
        String username = (String)fields[1].get(user);
        int age = (int)fields[2].get(user);
        String email = (String)fields[3].get(user);
        String telephone = (String)fields[4].get(user);

//        Table table = userClass.getAnnotation(Table.class);
//        String tableName = table.value();
//
//        Column column = fields[0].getAnnotation(Column.class);
//        String columnName = column.value();

        int num = 0;
        String result = "SELECT * FROM user WHERE";
        if(id != 0){
            if(num == 0){
                result += " `id` = "+ id;
            }else{
                result += " ,`id` = "+ id;
            }
            num = 1;
        }
        if(username != null){
            if(num == 0){
                result += " `username` LIKE '"+username+"'";
            }else{
                result += " ,`username` LIKE '"+username+"'";
            }
            num = 1;
        }
        if(age != 0){
            if(num == 0){
                result += " `age` = "+ age;
            }else{
                result += " ,`age` = "+ age;
            }
            num = 1;
        }
        if(email != null){
            if(num == 0){
                result += " `email` LIKE '"+email+"'";
            }else{
                result += " ,`email` LIKE '"+email+"'";
            }
            num = 1;
        }
        if(telephone != null){
            if(num == 0){
                result += " `telephone` LIKE '"+telephone+"'";
            }else{
                result += " ,`telephone` LIKE '"+telephone+"'";
            }
            num = 1;
        }


        return result;
    }

    @Override
    public String insert(User user) throws NoSuchFieldException, IllegalAccessException {
        Class<?> userClass = user.getClass();
        Field[] fields = userClass.getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
        }
        String username = (String)fields[1].get(user);
        int age = (int)fields[2].get(user);
        String email = (String)fields[3].get(user);
        String telephone = (String)fields[4].get(user);
        String result = "INSERT INTO `user` (`username`, `telephone`, `email`, `age`) VALUES ('"+username+"', '"+telephone+"', '"+email+"'," +age+")";
        return result;
    }

    @Override
    public String insert(List<User> users) throws IllegalAccessException {
        String results[] = new String[users.size()];
        for(int i = 0;i < users.size();i++){
            User user = users.get(i);
            Class<?> userClass = user.getClass();
            Field[] fields = userClass.getDeclaredFields();
            for(Field field:fields){
                field.setAccessible(true);
            }
            String username = (String)fields[1].get(user);
            int age = (int)fields[2].get(user);
            String email = (String)fields[3].get(user);
            String telephone = (String)fields[4].get(user);
            results[i] = "('"+username+"', '"+telephone+"', '"+email+"'," +age+")";
        }
        String result = "INSERT INTO `user` (`username`, `telephone`, `email`, `age`) VALUES ";
        for(int i = 0;i < users.size();i++){
            if(i != users.size()-1){
                result += results[i] + ",";
            }else {
                result += results[i];
            }
        }
        return result;
    }

    @Override
    public String delete(User user) throws NoSuchFieldException, IllegalAccessException {
        Class<?> userClass = user.getClass();
        Field field = userClass.getDeclaredField("id");
        field.setAccessible(true);
        int id = (int)field.get(user);
        String result = "DELETE FROM `user` WHERE `id` = " + id;
        return result;
    }

    @Override
    public String update(User user) throws IllegalAccessException {
        Class<?> userClass = user.getClass();
        Field[] fields = userClass.getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
        }
        int id = (int)fields[0].get(user);
        String username = (String)fields[1].get(user);
        int age = (int)fields[2].get(user);
        String email = (String)fields[3].get(user);
        String telephone = (String)fields[4].get(user);

//        Annotation annotation = fields[0].getAnnotation(Column.class);
//        String name = annotation.

        int num = 0;
        String result = "UPDATE `user` SET";
        if(username != null){
            if(num == 0){
                result += " `username` = '"+username+"'";
            }else{
                result += " ,`username` = '"+username+"'";
            }
            num = 1;
        }
        if(age != 0){
            if(num == 0){
                result += " `age` = "+ age;
            }else{
                result += " ,`age` = "+ age;
            }
            num = 1;
        }
        if(email != null){
            if(num == 0){
                result += " `email` = '"+email+"'";
            }else{
                result += " ,`email` = '"+email+"'";
            }
            num = 1;
        }
        if(telephone != null){
            if(num == 0){
                result += " `telephone` = '"+telephone+"'";
            }else{
                result += " ,`telephone` = '"+telephone+"'";
            }
            num = 1;
        }
        result += " WHERE `id` = "+ id;

        return result;
    }
}
